package com.tjsinfo.tjsoa.workplan.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanDefineBo;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanExamineBo;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanMain;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanExamine;

public class InstructionWorkPlanAction extends Action{
	private IWorkPlanExamineBo workPlanExamineBo;
	private IWorkPlanMainBo workPlanMainBo;
	private IWorkPlanDefineBo workPlanDefineBo;
	
	public IWorkPlanDefineBo getWorkPlanDefineBo() {
		return workPlanDefineBo;
	}

	public void setWorkPlanDefineBo(IWorkPlanDefineBo workPlanDefineBo) {
		this.workPlanDefineBo = workPlanDefineBo;
	}

	/**
	 * @return the workPlanMainBo
	 */
	public IWorkPlanMainBo getWorkPlanMainBo() {
		return workPlanMainBo;
	}

	/**
	 * @param workPlanMainBo the workPlanMainBo to set
	 */
	public void setWorkPlanMainBo(IWorkPlanMainBo workPlanMainBo) {
		this.workPlanMainBo = workPlanMainBo;
	}

	/**
	 * @return the workPlanExamineBo
	 */
	public IWorkPlanExamineBo getWorkPlanExamineBo() {
		return workPlanExamineBo;
	}

	/**
	 * @param workPlanExamineBo the workPlanExamineBo to set
	 */
	public void setWorkPlanExamineBo(IWorkPlanExamineBo workPlanExamineBo) {
		this.workPlanExamineBo = workPlanExamineBo;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String jihuaid = request.getParameter("id");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String value = request.getParameter("contest");
		if (workPlanExamineBo.saveORupdateWorkPlan(user.getUser_id(), jihuaid, value)) {
			WorkPlanMain workPlanMain = workPlanMainBo.selectByID(jihuaid);
			workPlanMain.setStart(2);
			workPlanMainBo.update(workPlanMain.getId(), workPlanMain.getTjsUser().getUserId(), workPlanMain.getTime(), workPlanMain.getType(), workPlanMain.getContent(), workPlanMain.getCompleteContent(), workPlanMain.getYear(), workPlanMain.getNumber(), workPlanMain.getStart());
			 int i = 1;
			List list =  workPlanDefineBo.selectDefine(user.getUser_id());
			if(list.size() == 0)
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkSuddenlyWorkPlan.do");
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				WorkPlanDefine	 workPlanDefine = (WorkPlanDefine) iterator.next();
				if(workPlanDefine.getTjsUser().getUserId().equals(workPlanMain.getTjsUser().getUserId()))
				{
					break;
				}
				if (!workPlanDefine.getTjsUser().getUserId().equals(workPlanMain.getTjsUser().getUserId()) && i == list.size()) {
					request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkSuddenlyWorkPlan.do");
					request.setAttribute("message", "workplan.message.check");
					return mapping.findForward("success");
				}
				i++;
			}
			if("D".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkDefalntWorkPlanDateModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			else if("W".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkDefalntWorkPlanWeekModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			else if("M".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkDefalntWorkPlanMonthModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			else if("Q".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkDefalntWorkPlanMonthJiDuModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
				
			}
			else if("H".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkworkPlanHalfYearModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			else if("Y".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/checkworkPlanYearModle.do?id="+workPlanMain.getTjsUser().getUserId());
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
				
			}
			else if("list".equals(workPlanMain.getType()))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanlistmode.do");
				request.setAttribute("message", "workplan.message.check");
				return mapping.findForward("success");
			}
			return mapping.findForward("okY");
			
		} else {
			return mapping.findForward("error");
		}
		
	}
	
}
